<?php

namespace App\Models;

use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole
{
    const OWNER = 'owner';

    protected $guarded = [];

    public static function getDeployRoleName($project_id, $setting_id)
    {
        $role_name = 'deploy_' . $project_id . '_' . $setting_id;
        if (!self::where('name', $role_name)->exists()) {
            self::create([
                'name'         => $role_name,
                'display_name' => $role_name,
                'description'  => 'project_id = ' . $project_id . ', setting_id = ' . $setting_id . '的角色（自动生成）',
            ]);
        }

        return $role_name;
    }

    public function users()
    {
        return $this->belongsToMany(User::class, 'role_user');
    }
}
